﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;

namespace E01
{
    /// <summary>
    /// 同步客户档案
    /// </summary>
    public class A05 : ISYNChildSet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String parameter)
        {
            List<E01A05E> E = new List<E01A05E>();
            E01A05P P = (E01A05P)handler.Serial.Deserialize(st, typeof(E01A05P), parameter);
            if (!P.ListIMEI.Equals("[]"))
            {
                E = (List<E01A05E>)handler.Serial.Deserialize(st, typeof(List<E01A05E>), P.ListIMEI);
            }

            try
            {
                SqlParameter[] sps = new SqlParameter[10 + E.Count * 2];
                sps[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 255);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@Number", SqlDbType.NVarChar, 255);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@BuildDate", SqlDbType.DateTime);
                sps[2].Value = Convert.ToDateTime(P.BuildDate);
                sps[3] = new SqlParameter("@CusName", SqlDbType.NVarChar, 255);
                sps[3].Value = P.CusName;
                sps[4] = new SqlParameter("@PassWord", SqlDbType.NVarChar, 255);
                sps[4].Value = P.PassWord;
                sps[5] = new SqlParameter("@LinkMan", SqlDbType.NVarChar, 255);
                sps[5].Value = P.LinkMan;
                sps[6] = new SqlParameter("@Remark", SqlDbType.NVarChar, 255);
                sps[6].Value = P.Remark;
                sps[7] = new SqlParameter("@IsApp", SqlDbType.Bit);
                sps[7].Value = P.IsApp;
                sps[8] = new SqlParameter("@IsIMEI", SqlDbType.Bit);
                sps[8].Value = P.IsIMEI;
                sps[9] = new SqlParameter("@MultiCheckStatus", SqlDbType.Int);
                sps[9].Value = P.MultiCheckStatus;
                for (int i = 0; i < E.Count; i++)
                {
                    sps[10 + 2 * i] = new SqlParameter("@Index" + i.ToString(), SqlDbType.NChar, 50);
                    sps[10 + 2 * i].Value = E[i].Index;
                    sps[11 + 2 * i] = new SqlParameter("@IMEI" + i.ToString(), SqlDbType.NChar, 50);
                    sps[11 + 2 * i].Value = E[i].IMEI;
                }

                StringBuilder sql = new StringBuilder();
                sql.Append("SET NOCOUNT ON\r\n");
                sql.Append("DECLARE @CusNameFID INT\r\n SET @CusNameFID = IsNull((SELECT FItemID FROM t_Organization WHERE FHomePage=@CusName AND FHomePage<>''),0)\r\n");
                sql.Append("DECLARE @FID INT\r\n SET @FID = IsNull((SELECT FID FROM KEW_SalesGuests WHERE FText3 =@ID),0)\r\n");
                sql.Append("IF @FID>0 BEGIN\r\n");
                sql.Append("  UPDATE KEW_SalesGuests SET FBase=@CusNameFID,FDate=@BuildDate,FText=@PassWord,FText1=@LinkMan,FNOTE=@Remark,FCheckBox=@IsApp,FCheckBox1=@IsIMEI WHERE FID=@FID\r\n");
                sql.Append("  SELECT 'U',@FID\r\n");
                sql.Append("END \r\nELSE BEGIN\r\n");
                sql.Append("  EXEC GetICMaxNum 'KEW_SalesGuests',@FID OUTPUT\r\n");
                sql.Append("  INSERT INTO KEW_SalesGuests ([FClassTypeID],[FID],[FBillNo],[FText],[FBase],[FDate],[FCheckBox],[FCheckBox1],[FConnectFlag],[FBiller],[FBase1],[FText1],[FNOTE],[FBase2],[FBase3],[FUser],[FMultiCheckStatus],[FText3]) ");
                sql.Append("  VALUES (236900152,@FID,@Number,@PassWord,@CusNameFID,@BuildDate,@IsApp,@IsIMEI,0,0,0,'','',0,0,0,16,@ID)\r\n");
                sql.Append("  SELECT 'I',@FID\r\n");
                sql.Append("END \r\n");
                for (int i = 0; i < E.Count; i++)
                {
                    sql.Append("IF (SELECT COUNT(1) FROM KEW_SalesGuestsIMEI WHERE FID =@FID AND FIndex=@Index" + i.ToString() + " ) > 0 BEGIN \r\n ");
                    sql.Append("  UPDATE KEW_SalesGuestsIMEI SET FText2=@IMEI" + i.ToString() + " WHERE FID =@FID AND FIndex=@Index" + i.ToString() + " \r\n");
                    sql.Append("END \r\nELSE BEGIN\r\n");
                    sql.Append("  INSERT INTO KEW_SalesGuestsIMEI ([FID],[FText2],[FIndex]) VALUES (@FID,@IMEI" + i.ToString() + ",@Index" + i.ToString() + ")\r\n ");
                    sql.Append("END \r\n");
                }

                sql.Append("SET NOCOUNT OFF\r\n");
                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql.ToString(), sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "U")
                    {
                        result.Result = EnumResult.Update;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    else
                    {
                        result.Result = EnumResult.Insert;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    result.Message = String.Format("客户档案同步完成:{0},{1}", P.Number, result.Result.ToString());
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("客户档案同步失败:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("客户档案同步失败:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 客户档案
    /// </summary>
    public class E01A05P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 三方名称
        /// </summary>
        //public String Name;
        /// <summary>
        /// 建档日期
        /// </summary>
        public String BuildDate;
        /// <summary>
        /// 客户名称
        /// </summary>
        public String CusName;
        /// <summary>
        /// 密码
        /// </summary>
        public String PassWord;
        /// <summary>
        /// 联系人
        /// </summary>
        public String LinkMan;
        /// <summary>
        /// 备注
        /// </summary>
        public String Remark;
        /// <summary>
        /// 是否允许手机登录
        /// </summary>
        public Boolean IsApp;
        /// <summary>
        /// 是否只允许IMEI列表的设备访问
        /// </summary>
        public Boolean IsIMEI;
        /// <summary>
        /// 审批流程状态（）
        /// </summary>
        public int MultiCheckStatus;
        /// <summary>
        /// 
        /// </summary>
        public String ListIMEI;
     
    }
    /// <summary>
    /// 客户档案-IMEI列表
    /// </summary>
    public class E01A05E
    {

        /// <summary>
        /// 设备IMEI
        /// </summary>
        public String IMEI;
        /// <summary>
        /// 设备IMEI顺序号
        /// </summary>
        public int Index;


    }
}
